package com.cnc.bas.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

import com.cnc.bas.util.Constants;
import com.cnc.bas.util.DataCache;
import com.cnc.bas.util.LTFormatUtil;
import com.cnc.bas.util.Log;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.UserInfo;


/**
 * @author Du bf
 * @version 1.0  2005-3-29
 */

public final class LogonAction extends BasAction {


    private Log log = Log.getLogger(LogonAction.class);

    private ISsmService service = null;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        service = (ISsmService) this.getBean(Constants.BEAN_NAME_SSM);
    }
    
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
    	String local = request.getLocale().toString();
    	if(local.indexOf("en")!=-1) local = "en"; 
        else local="zh_CN";
    	HttpSession session = request.getSession();    
        if (session != null) session.invalidate();
        session = request.getSession(true);
        String username = (String) PropertyUtils.getSimpleProperty(form, "userName");
        String password = (String) PropertyUtils.getSimpleProperty(form, "password");
        String forwardAction = (String) PropertyUtils.getSimpleProperty(form, "forwardAction");
        ISsmService ssmService = (ISsmService) this.getBean(Constants.BEAN_NAME_SSM);        
        UserInfo oper = ssmService.login(username, password);       
        session.setAttribute(Constants.USER_KEY, oper);        
        session.setAttribute(Constants.SYSTEM_DATE, LTFormatUtil.formatDate());
        session.setAttribute(Constants.AUTH_LIST, oper.getUserPermission());        		
        if (mapping.getAttribute() != null) {
            if ("request".equals(mapping.getScope()))
                request.removeAttribute(mapping.getAttribute());
            else
                session.removeAttribute(mapping.getAttribute());
        }
        long loginId = ssmService.addLoginInfo(oper, request.getRemoteAddr());        
        session.setAttribute(Constants.LOGIN_ID, loginId);
        String strUserCode = oper.getUserCode();        
        List menuList = DataCache.getMenuList(strUserCode,local);  
        session.setAttribute("menuList", menuList);       
        if (forwardAction == null || forwardAction.length() < 1){             
            return mapping.findForward("main");
        } else {
            response.sendRedirect(forwardAction);
            return null;
        }
        
    }
}
